package com.Shoping.controller;

import com.Shoping.entity.ShoppingList;
import com.Shoping.util.DbUtils;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 该实现类将会将购物车的内容以list的形式返回到前端
 * 具体实现方式，使用用户id来查购物车id，然后来查对应的商品块
 */
@WebServlet(value = "/shoppingList")
public class ShoppingListController extends HttpServlet {

    Connection connection = DbUtils.getConnection();
    ResultSet rs;
    PreparedStatement ps;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer uid = Integer.valueOf(req.getParameter("uId"));//获取用户id
        Integer sid = 0;//购物车id

        //判断用户id是否为空，若为空，则需要注册
        if (uid == null) {
            /**
             * 此处要重定向到登录页
             */
            resp.sendRedirect("/ProgramJSP/register.jsp");
        } else {
            try {
                ps = connection.prepareStatement("select s_id from shopping where u_id=" + uid);
                rs = ps.executeQuery();

                if (rs.next()) {
                    sid = rs.getInt("sId");
                } else {
                    System.out.println("ShoppingListController---------------------------没有找到sId");
                    return;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingListController---------------------------获取购物车id错误!");
            }

            /**
             * 找到购物车id之后，去找商品块
             */
            List<ShoppingList> shoppingLists = new ArrayList<ShoppingList>();
            try {
                ps = connection.prepareStatement("select * from commoditys where s_id=" + sid);
                rs = ps.executeQuery();

                while (rs.next()) {
                    ResultSet resultSet = null;

                    //通过商品主键来查询商品价格和名字
                    ps = connection.prepareStatement("select cm_price,cm_name from commodity where cm_id=" + rs.getInt("cm_id"));

                    ShoppingList shop = new ShoppingList(
                            rs.getInt("cms_id"),
                            rs.getString("o_number"),
                            rs.getInt("cm-id"),
                            rs.getInt("s-id"),
                            rs.getInt("cms-number"),
                            rs.getDouble("cms-subtotal"),
                            resultSet.getString("cm-name"),
                            resultSet.getDouble("cm-price"),
                            rs.getString("cms-colorName")
                    );
                    shoppingLists.add(shop);
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingListController---------------------------获取商品块id错误!");
            }

            String s= JSON.toJSONString(shoppingLists);
            resp.getWriter().write(s);
            req.getRequestDispatcher("/ProgramJSP/gwc.jsp").forward(req, resp);
        }
    }
}
